package servletTestOne;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

public class userServlet extends HttpServlet {
	List<User> userList = new ArrayList<>();
	private Long count = 1L;
	private String user;
	private long id;

	@Override
	public void init() throws ServletException {
		System.out.println("初始化.........");
		userList.add(new User(count++, "zhangsan", "123456", "123456", new Date(), new Date(), "N"));
		userList.add(new User(count++, "lisi", "123457", "223456", new Date(), new Date(), "N"));
		userList.add(new User(count++, "wanwu", "123458", "323456", new Date(), new Date(), "N"));
		userList.add(new User(count++, "liuming", "123459", "423456", new Date(), new Date(), "N"));
		userList.add(new User(count++, "maliu", "1234510", "523456", new Date(), new Date(), "N"));
	}

	@Override
	public void service(ServletRequest req, ServletResponse response) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		System.out.println(method);
		if ("add".equals(method)) {
			String userName = req.getParameter("userName");
			String password = req.getParameter("password");
			String phone = req.getParameter("phone");
			String DeleteLogo = req.getParameter("DeleteLogo");
			userList.add(new User(count++, userName, password, phone, new Date(), new Date(), DeleteLogo));
			list(response);
		} else if ("update".equals(method)) {
			update(req, response);
			list(response);
		} else if ("delete".equals(method)) {
			delete(req);
			list(response);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, response);
		} else if ("List".equals(method)) {
			list(response);
		} else {
			list(response);
		}
	}

	private void update(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(req.getParameter("user"));
		System.out.println("update");

		Long id = Long.valueOf(req.getParameter("id"));
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		userList.forEach(e->{
			if(e.getId().equals(id)) {
				e.setUserName(userName);
				e.setPassword(password);
				e.setPhone(phone);
				//修改时间是当前时间
				e.setUpdateTime(new Date());
			}
		});
	}

	private void toUpdate(ServletRequest req, ServletResponse response) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		User us = null;
		Iterator<User> iterator = userList.iterator();
		while (iterator.hasNext()) {
			us = iterator.next();
			// object
			if (us.getId().equals(id)) {
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				out.println("<form action=\"mySevletTese?method=update\" method=\"post\">");
				out.println("<input type='hidden' name='id' value='" + us.getId() + "'><p>");
				out.println("<input name='userName' value='" + us.getUserName() + "'><p>");
				out.println("<input name='phone' value='" + us.getPhone() + "'>");
				out.println("<input name='password' value='" + us.getPassword() + "'>");
				out.println("<input name='creationTime' value='" + us.getCreationTime() + "'>");
				out.println("<input name='updateTime'value='" + us.getUpdateTime() + "'>");
				out.println("<input type=\"submit\" value=\"提交\">\r\n" + "</form>");
			}
		}
	}

	private void delete(ServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		userList.forEach(e -> {
			if (e.getId().equals(id)) {
				e.setDeleteLogo("Y");
			}
		});
	}

	private void list(ServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>" + user + "</h1>");
		out.println("<a href='test/testOne.html'>添加</a>");
		out.println("<table border=1>");
		out.print("<tr><td>id</td><td>姓名</td><td>电话</td><td>密码</td><td>创建时间</td><td>修改时间</td><tr>");
		for (User us : userList) {
			if ("Y".equals(us.getDeleteLogo())) {
				continue;
			}
			out.println("<tr>");
			out.println("<td>");
			out.print(us.getUserName());
			out.print("</td>");
			out.print("<td>");
			out.print(us.getPassword());
			out.print("</td");
			out.print("<td>");
			out.print(us.getPhone());
			out.print("</td>");
//			out.print("<td>");
//			out.print(us.creationTime);
//			out.print("</td>");
//			out.print("<td>");
//			out.print(us.getDeleteLogo());
//			out.print("</td>");
//			out.print("<td>");
//			out.print(us.getUpdateTime());
//			out.print("</td>");
			out.print("<td>");
			out.print("<a href='mySevletTese?method=delete&id=" + us.getId() + "'>删除</a>    ");
			out.print("<a href='mySevletTese?method=toUpdate&id=" + us.getId() + "'>修改</a>");
			out.println("</td>");
			out.print("</tr>");
		}
		out.println("</table>");
	}

	@Override
	public void destroy() {
		System.out.println("servlet销毁........");
		super.destroy();
	}

	class User {
		private Long id;
		private String userName;
		private String password;
		private String phone;
		private Date creationTime;
		private Date updateTime;
		private String DeleteLogo;

		public Long getId() {
			return id;
		}

		public void setId(Long id) {
			this.id = id;
		}

		public String getUserName() {
			return userName;
		}

		public void setUserName(String userName) {
			this.userName = userName;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getPhone() {
			return phone;
		}

		public void setPhone(String phone) {
			this.phone = phone;
		}

		public Date getCreationTime() {
			return creationTime;
		}

		public void setCreationTime(Date creationTime) {
			this.creationTime = creationTime;
		}

		public Date getUpdateTime() {
			return updateTime;
		}

		public void setUpdateTime(Date updateTime) {
			this.updateTime = updateTime;
		}

		public String getDeleteLogo() {
			return DeleteLogo;
		}

		public void setDeleteLogo(String deleteLogo) {
			DeleteLogo = deleteLogo;
		}

		public User() {

		}

		public User(long id, String userName, String password, String phone, Date creationTime, Date updateTime,
				String deleteLogo) {
			super();
			this.id = id;
			this.userName = userName;
			this.password = password;
			this.phone = phone;
			this.creationTime = creationTime;
			this.updateTime = updateTime;
			DeleteLogo = deleteLogo;
		}

	}
}
